﻿using log4net;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WMSSyncData.DAL;
using WMSSyncData.DAL.Oracle;
using WMSSyncData.DAL.SqlDB;
using WMSSyncData.Model;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.Core
{
    public class SyncERPDataSalePlan
    {

        private readonly ILog _logger = LogManager.GetLogger(typeof(SyncERPData));
        public void SyncOutStockData()
        {
            Tddev121DAL tddev111DAL = new Tddev121DAL();
            DAL.Oracle.WMS_T_OutStockRowsDAL wMS_T_OutStockRowsDAL = new DAL.Oracle.WMS_T_OutStockRowsDAL();

            var list = tddev111DAL.GetAll();
            if (list.Count > 0)
            {

                var rowsList = wMS_T_OutStockRowsDAL.GetAllSalePlan();
                var cloneList = Clone(rowsList);
                var execRes = 0;
                if (list.Count > 0)
                {
                    execRes = new BaseSqlDBDAL<WMS_T_OutStock>().InsertBatch(list);
                }

                var addRowsList = FormatRowsList(rowsList, list);
                var execRowsRes = 0;
                if (addRowsList.Count > 0)
                {
                    execRowsRes = new BaseSqlDBDAL<WMS_T_OutStockRows>().InsertBatch(addRowsList);
                }
 
                if (cloneList.Count > 0)
                {
                  var resultclone=  wMS_T_OutStockRowsDAL.UpdateHostSalePlan(cloneList);
                }
            }
        }

        public static T Clone<T>(T RealObject)
        {
            using (Stream objectStream = new MemoryStream())
            {
                //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制  
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objectStream, RealObject);
                objectStream.Seek(0, SeekOrigin.Begin);
                return (T)formatter.Deserialize(objectStream);
            }
        }
        public List<WMS_T_OutStockRows> FormatRowsList(List<WMS_T_OutStockRows> rowsList, List<WMS_T_OutStock> list)
        {
            var result = new List<WMS_T_OutStockRows>();
            foreach (var item in rowsList)
            {
                //从Oracle数据库获取ROWS数据的时候，把RelationBill放到了BillNO字段上面，现在通过这个字段关联，获取真正的BillNO
                var searchItem = list.Where(x => x.RelatedBill.EqualsOrdinalIgnoreCase(item.BillNo)
                && x.SN == item.SN && x.RowNo == item.RowNo && x.SetNo == item.SetNo).FirstOrDefault();
                if (searchItem != null)
                {
                    item.BillNo = searchItem.BillNo;
                    result.Add(item);
                }
            }
            return result;
        }

        public void OutStockMSyncSalePlan()
        {
            Tddev119DAL tddev119DAL = new Tddev119DAL();
            var list = tddev119DAL.GetSyncData();
            if (list.Count > 0)
            {
                var result = new BaseOracleDAL<tddev119>().InsertBatch(list);

                if (result > 0)
                {
                    tddev119DAL.UpdateUploadStatus(list);
                }
            }
        }

    }
}
